
runmodel <- function(x, data = dx2 %>% filter(smpl == 1)
                     , kcx = FALSE) {
    r <- vector("list", 1)
    for(i in 1:length(x)) {
        r[[i]] <- felm(as.formula(x[[i]]), data = data, keepCX = kcx)
    }
    r
}

trg <- function(x, showoutput = F, clevel = .95, ...)
{
    texreg(
        x
        , ci.force = TRUE
        , ci.force.level = clevel
        # , omit.coef = "X|Intercept"
        , caption.above = TRUE
        , digits = 2
        , controls = TRUE
        , booktabs = TRUE
        , dcolumn = FALSE
        , use.packages = FALSE
        , float.pos = "bh"
        # , custom.note.wrap = 99
        # , fontsize = "scriptsize"
        , scalebox = .75
        # , custom.note = gsub("; ", paste0(";", get(paste0("brk", length(x) + 1))), cnt)
        , file = paste0(workdir, "tables/", lbl, ".tex")
        , label = paste0("t:", lbl)
        , ...
    )

    if (showoutput) {
        screenreg(x
                  , ci.force = TRUE
                  , ci.force.level = clevel
                  , digits = 2
                  , controls = TRUE)
    }
}

brk3 <- "\\}\\} \\\\\\\\ \\\\multicolumn\\{3\\}\\{l\\}\\{\\\\footnotesize\\{"
brk4 <- "\\}\\} \\\\\\\\ \\\\multicolumn\\{4\\}\\{l\\}\\{\\\\footnotesize\\{"
brk5 <- "\\}\\} \\\\\\\\ \\\\multicolumn\\{5\\}\\{l\\}\\{\\\\footnotesize\\{"
brk6 <- "\\}\\} \\\\\\\\ \\\\multicolumn\\{6\\}\\{l\\}\\{\\\\footnotesize\\{"
brk7 <- "\\}\\} \\\\\\\\ \\\\multicolumn\\{7\\}\\{l\\}\\{\\\\footnotesize\\{"

cnt <- paste0("*) 95\\% confidence interval ",
                               "clustered at the country level ",
                               "(in brackets) does not include zero; ",
              "dependent variable: V-Dem electoral democracy index ",
              "scaled to a range of [0;\\enspace100]; ",
                 "all explanatory variables lagged by one period."
              )
cnta <- paste0("*) 95\\% confidence interval ",
               "clustered at the country level ",
               "(in brackets) does not include zero; ",
              "dependent variable: V-Dem electoral democracy index ",
              "scaled to a range of [0;\\enspace100]; ",
               "additional controls include population (log), GDP per ",
               "capita (log) and a civil conflict dummy; ",
               "all explanatory variables lagged by one period."
              )

trgedit <- function(x = paste0(workdir, "tables/", lbl, ".tex")) {
    cat(
        # sub("\\", "\\\\",
        sub("R\\$", "\\\\hline R\\$",
            gsub("9999999", "\\\\textit{no}",
                 gsub("1111111", "\\\\textit{yes}",
                      sub("scriptsize", "footnotesize",
                           # x
                           readLines(x)
                           # , fixed = TRUE
                      )
                      #                 gsub("9999999", "\\\\textit{no}",
                      # gsub("1111111", "\\\\textit{yes}",
                      #      readLines(x)
                      #      # , fixed = TRUE
                      # )
                 )
            )
            # )
        )
    )
}

sr <- function(x = "r", ...) {
    screenreg(get(x), omit.coef = "cnamef|periodf|pop|gdpcap|war25", ...)
}

log10.axis <- function(side, at, #lab,
                       ...) {
    at.minor <- log10(outer(1:9, 10^(min(at):max(at))))
    # if (is.null(lab)) {
    # lab <- sapply(at, function(i) as.expression(bquote(10^ .(i)))) }
    axis(side=side, at=at.minor, labels=NA, tcl=par("tcl")*0.5, ...)
    axis(side=side, at=at, labels=NA, ...)
}

